/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cnam.mobileShopping.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import org.cnam.mobileShopping.entity.User;

/** Jersey REST client generated for REST resource:UserResource [user]<br>
 *  USAGE:<pre>
 *        NewJerseyClient client = new NewJerseyClient();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 *  </pre>
 * @author Administrateur
 */

/**
 * besoin des librairies jersey$
 * si on veut pas utiliser jersey, il faut utiliser org.json C:\Documents and Settings\Administrateur\Mes documents\NetBeansProjects\NBMediaPlatform\AndroidMedia\src\org\pfabier\android\media\entity\music
 * wsclient et wshelper dans le package wsclient
 * 
 * @author Administrateur
 */
public class NewJerseyClient {
    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:8080/MobileShopping_WS/resources";

    public NewJerseyClient() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("user");
    }

    public User creerUtilisateur(String pseudo, String login, String password) throws UniformInterfaceException {
        WebResource resource = webResource;
        if (pseudo != null) {
            resource = resource.queryParam("pseudo", pseudo);
        }
        if (login != null) {
            resource = resource.queryParam("login", login);
        }
        if (password != null) {
            resource = resource.queryParam("password", password);
        }
        resource = resource.path("creerUtilisateur");
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(User.class);
    }

    public <T> T login(Class<T> responseType, String login, String password) throws UniformInterfaceException {
        WebResource resource = webResource;
        if (login != null) {
            resource = resource.queryParam("login", login);
        }
        if (password != null) {
            resource = resource.queryParam("password", password);
        }
        resource = resource.path("auth");
        return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
    }

    public void close() {
        client.destroy();
    }
    
}
